/**
 * Created with JetBrains PhpStorm.
 * User: Bezdelnick
 * Date: 11.04.13
 * Time: 16:46
 * To change this template use File | Settings | File Templates.
 */
var addActorDialog;
var editActorDialog;
var removeActorDialog;

var addCountryDialog;
var editCountryDialog;
var removeCountryDialog;

var addGenreDialog;
var editGenreDialog;
var removeGenreDialog;

var addFilmDialog;
var editFilmDialog;
var removeFilmDialog;

var addNewsDialog;
var editNewsDialog;
var removeNewsDialog;

function createLinks() {
    var addActorLink = $('#add_actor_link').click(function () {
        $('input', addActorDialog).val('');
        addActorDialog.dialog('open');
    });
    var editActorLink = $('.edit-actor-link').click(function () {
        var currentRow = $(this).parent().parent();
        var actorId = $(this).attr('itemId');
        var actorFirstName = $('td:eq(1)', currentRow).text();
        var actorLastName = $('td:eq(2)', currentRow).text();
        $('input[name=edit_actor_id]', editActorDialog).val(actorId);
        $('input[name=edit_actor_first_name]', editActorDialog).val(actorFirstName);
        $('input[name=edit_actor_last_name]', editActorDialog).val(actorLastName);
        editActorDialog.dialog('open');
    });
    var removeActorLink = $('.remove-actor-link').click(function () {
        var currentRow = $(this).parent().parent();
        var actorId = $(this).attr('itemId');
        var actorFirstName = $('td:eq(1)', currentRow).text();
        var actorLastName = $('td:eq(2)', currentRow).text();
        $('input[name=remove_actor_id]', removeActorDialog).val(actorId);
        $('label[name=remove_actor_first_name]', removeActorDialog).text(actorFirstName);
        $('label[name=remove_actor_last_name]', removeActorDialog).text(actorLastName);
        removeActorDialog.dialog('open');
    });

    var addCountryLink = $('#add_country_link').click(function () {
        $('input', addCountryDialog).val('');
        addCountryDialog.dialog('open');
    });
    var editCountryLink = $('.edit-country-link').click(function () {
        var currentRow = $(this).parent().parent();
        var countryId = $(this).attr('itemId');
        var countryName = $('td:eq(1)', currentRow).text();
        $('input[name=edit_country_id]', editCountryDialog).val(countryId);
        $('input[name=edit_country_first]', editCountryDialog).val(countryName);
        editCountryDialog.dialog('open');
    });
    var removeCountryLink = $('.remove-country-link').click(function () {
        var currentRow = $(this).parent().parent();
        var countryId = $(this).attr('itemId');
        var countryName = $('td:eq(1)', currentRow).text();
        $('input[name=remove_country_id]', removeCountryDialog).val(countryId);
        $('label[name=remove_country_name]', removeCountryDialog).text(countryName);
        removeCountryDialog.dialog('open');
    });

    var addGenreLink = $('#add_genre_link').click(function () {
        $('input', addGenreDialog).val('');
        addGenreDialog.dialog('open');
    });
    var editGenreLink = $('.edit-genre-link').click(function () {
        var currentRow = $(this).parent().parent();
        var genreId = $(this).attr('itemId');
        var genreName = $('td:eq(1)', currentRow).text();
        $('input[name=edit_genre_id]', editGenreDialog).val(genreId);
        $('input[name=edit_genre_name]', editGenreDialog).val(genreName);
        editGenreDialog.dialog('open');
    });
    var removeGenreLink = $('.remove-genre-link').click(function () {
        var currentRow = $(this).parent().parent();
        var genreId = $(this).attr('itemId');
        var genreName = $('td:eq(1)', currentRow).text();
        $('input[name=remove_genre_id]', removeGenreDialog).val(genreId);
        $('label[name=remove_genre_name]', removeGenreDialog).text(genreName);
        removeGenreDialog.dialog('open');
    });

    var addFilmLink = $('#add_film_link').click(function () {
        $('input', addFilmDialog).val('');
        addFilmDialog.dialog('open');
    });
    var editFilmLink = $('.edit-film-link').click(function () {
        var currentRow = $(this).parent().parent();
        var filmId = $(this).attr('itemId');
        var filmName = $('td:eq(1)', currentRow).text();
        var filmDescription = $('td:eq(2)', currentRow).text();
        var filmYear = $('td:eq(3)', currentRow).text();
        $('input[name=edit_film_id]', editFilmDialog).val(filmId);
        $('input[name=edit_film_name]', editFilmDialog).val(filmName);
        $('input[name=edit_film_description]', editFilmDialog).val(filmDescription);
        $('input[name=edit_film_year]', editFilmDialog).val(filmYear);
        editFilmDialog.dialog('open');
    });
    var removeFilmLink = $('.remove-film-link').click(function () {
        var currentRow = $(this).parent().parent();
        var filmId = $(this).attr('itemId');
        var filmName = $('td:eq(1)', currentRow).text();
        var filmDescription = $('td:eq(2)', currentRow).text();
        var filmYear = $('td:eq(3)', currentRow).text();
        $('input[name=remove_film_id]', removeFilmDialog).val(filmId);
        $('label[name=remove_film_name]', removeFilmDialog).text(filmName);
        $('label[name=remove_film_description]', removeFilmDialog).text(filmDescription);
        $('label[name=remove_film_year]', removeFilmDialog).text(filmYear);
        removeGenreDialog.dialog('open');
    });

    var addNewsLink = $('#add_news_link').click(function () {
        $('input', addNewsDialog).val('');
        addNewsDialog.dialog('open');
    });
    var editNewsLink = $('.edit-news-link').click(function () {
        var currentRow = $(this).parent().parent();
        var newsId = $(this).attr('itemId');
        var newsName = $('td:eq(1)', currentRow).text();
        var newsDescription = $('td:eq(2)', currentRow).text();
        var newsPublDate = $('td:eq(3)', currentRow).text();
        $('input[name=edit_news_id]', editNewsDialog).val(newsId);
        $('input[name=edit_news_name]', editNewsDialog).val(newsName);
        $('input[name=edit_news_description]', editNewsDialog).val(newsDescription);
        $('input[name=edit_news_publ_date]', editNewsDialog).val(newsPublDate);
        editNewsDialog.dialog('open');
    });
    var removeNewsLink = $('.remove-news-link').click(function () {
        var currentRow = $(this).parent().parent();
        var filmId = $(this).attr('itemId');
        var filmName = $('td:eq(1)', currentRow).text();
        var filmDescription = $('td:eq(2)', currentRow).text();
        var filmYear = $('td:eq(3)', currentRow).text();
        $('input[name=remove_news_id]', removeNewsDialog).val(filmId);
        $('label[name=remove_news_name]', removeNewsDialog).text(filmName);
        $('label[name=remove_news_description]', removeNewsDialog).text(filmDescription);
        removeNewsDialog.dialog('open');
    });


}

$(function () {
    // ==================== Add Actor ==================== //
    addActorDialog = getJqueryDialog('add_actor_dialog', {
        'ok':function () {
            $.ajax({
                type:'POST',
                url:$('#add_actor_form').attr('action'),
                data:getSerialisedForm('#add_actor_form', 'add'),
                success:function (response) {
                    updateContentContainer(actorContentContainer, '');
                }
            });
        },
        'cancel':function () {
        }
    });
    // ==================== Edit Actor dialog ==================== //
    editActorDialog = getJqueryDialog('edit_actor_dialog', {
        'ok':function () {
            $.ajax({
                type:"POST",
                url:$('#edit_actor_form').attr('action'),
                data:getSerialisedForm('#edit_actor_form', 'edit'),
                success:function (response) {
                    updateContentContainer(actorContentContainer, '');
                }
            });
        },
        'cancel':function () {
        }
    });
    // ==================== Remove Actor dialog ==================== //
    removeActorDialog = getJqueryDialog('remove_actor_dialog', {
        'ok':function () {
            $.ajax({
                type:'POST',
                url:$('#remove_actor_form').attr('action'),
                data:getSerialisedForm('#remove_actor_form', 'remove'),
                success:function (response) {
                    updateContentContainer(actorContentContainer, '');
                }
            });
        },
        'cancel':function () {
        }
    });


    // ==================== Add Country ==================== //
    addCountryDialog = getJqueryDialog('add_country_dialog', {
        'ok':function () {
            $.ajax({
                type:'POST',
                url:$('#add_country_form').attr('action'),
                data:getSerialisedForm('#add_country_form', 'add'),
                success:function (response) {
                    updateContentContainer(countryContentContainer, '');
                }
            });
        },
        'cancel':function () {
        }
    });
    // ==================== Edit Country dialog ==================== //
    editCountryDialog = getJqueryDialog('edit_country_dialog', {
        'ok':function () {
            $.ajax({
                type:'POST',
                url:$('#edit_country_form').attr('action'),
                data:getSerialisedForm('#edit_country_form', 'edit'),
                success:function (response) {
                    updateContentContainer(countryContentContainer, '');
                }
            });
        },
        'cancel':function () {
        }
    });
    // ==================== Remove Country dialog ==================== //
    removeCountryDialog = getJqueryDialog('remove_country_dialog', {
        'ok':function () {
            $.ajax({
                type:'POST',
                url:$('#remove_country_form').attr('action'),
                data:getSerialisedForm('#remove_country_form', 'remove'),
                success:function (response) {
                    updateContentContainer(countryContentContainer, '');
                }
            });
        },
        'cancel':function () {
        }
    });


    // ==================== Add Genre ==================== //
    addGenreDialog = getJqueryDialog('add_genre_dialog', {
        'ok':function () {
            $.ajax({
                type:'POST',
                url:$('#add_genre_form').attr('action'),
                data:getSerialisedForm('#add_genre_form', 'add'),
                success:function (response) {
                    updateContentContainer(genreContentContainer, '');
                }
            });
        },
        'cancel':function () {
        }
    });
    // ==================== Edit Genre dialog ==================== //
    editGenreDialog = getJqueryDialog('edit_genre_dialog', {
        'ok':function () {
            $.ajax({
                type:'POST',
                url:$('#edit_genre_form').attr('action'),
                data:getSerialisedForm('#edit_genre_form', 'edit'),
                success:function (response) {
                    updateContentContainer(genreContentContainer, '');
                }
            });
        },
        'cancel':function () {
        }
    });
    // ==================== Remove Genre dialog ==================== //
    removeGenreDialog = getJqueryDialog('remove_genre_dialog', {
        'ok':function () {
            $.ajax({
                type:'POST',
                url:$('#remove_genre_form').attr('action'),
                data:getSerialisedForm('#remove_genre_form', 'remove'),
                success:function (response) {
                    updateContentContainer(genreContentContainer, '');
                }
            });
        },
        'cancel':function () {
        }
    });


    // ==================== Add Film ==================== //
    addFilmDialog = getJqueryDialog('add_film_dialog', {
        'ok':function () {
            var genres_array = $('[name="film_genres[]"]').multiselect("getChecked").map(function () {
                return "{obj_" +this.value + ":" + this.value + "}"; }).get();
            var actors_array = $('[name="film_actors[]"]').multiselect("getChecked").map(function () {
                return "{obj_" +this.value + ":" + this.value + "}"; }).get();
            var countries_array = $('[name="film_countries[]"]').multiselect("getChecked").map(function () {
                return "{obj_" +this.value + ":" + this.value + "}"; }).get();

            var sendData = getSerialisedForm('#add_film_form', 'add');// +
            //alert('sendData = ' + sendData);

            $.ajax({
                type:'POST',
                url:$('#add_film_form').attr('action'),
                data:sendData,
                success:function (response) {
                    updateContentContainer(filmContentContainer, '');
                    console.log('response = ' + response);
                }
            });
        },
        'cancel':function () {
        }
    });
    // ==================== Edit Film dialog ==================== //
    editFilmDialog = getJqueryDialog('edit_film_dialog', {
        'ok':function () {
            $.ajax({
                type:'POST',
                url:$('#edit_film_form').attr('action'),
                data:getSerialisedForm('#edit_film_form', 'edit'),
                success:function (response) {
                    updateContentContainer(filmContentContainer, '');
                }
            });
        },
        'cancel':function () {
        }
    });
    // ==================== Remove Film dialog ==================== //
    removeFilmDialog = getJqueryDialog('remove_film_dialog', {
        'ok':function () {
            $.ajax({
                type:'POST',
                url:$('#remove_film_form').attr('action'),
                data:getSerialisedForm('#remove_film_form', 'remove'),
                success:function (response) {
                    updateContentContainer(filmContentContainer, '');
                }
            });
        },
        'cancel':function () {
        }
    });


    // ==================== Add News ==================== //
    addNewsDialog = getJqueryDialog('add_news_dialog', {
        'ok':function () {
            $.ajax({
                type:'POST',
                url:$('#add_news_form').attr('action'),
                data:getSerialisedForm('#add_news_form', 'add'),
                success:function (response) {
                    updateContentContainer(newsContentContainer, '');
                }
            });
        },
        'cancel':function () {
        }
    });
    // ==================== Edit News dialog ==================== //
    editNewsDialog = getJqueryDialog('edit_news_dialog', {
        'ok':function () {
            $.ajax({
                type:'POST',
                url:$('#edit_news_form').attr('action'),
                data:getSerialisedForm('#edit_news_form', 'edit'),
                success:function (response) {
                    updateContentContainer(newsContentContainer, '');
                }
            });
        },
        'cancel':function () {
        }
    });
    // ==================== Remove News dialog ==================== //
    removeNewsDialog = getJqueryDialog('remove_news_dialog', {
        'ok':function () {
            $.ajax({
                type:'POST',
                url:$('#remove_news_form').attr('action'),
                data:getSerialisedForm('#remove_news_form', 'remove'),
                success:function (response) {
                    updateContentContainer(newsContentContainer, '');
                }
            });
        },
        'cancel':function () {
        }
    });

    createLinks();
})